package net.xxcl.kygl.controller;

import net.xxcl.kygl.pojo.Changerank;
import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.User;
import net.xxcl.kygl.service.ChangerankService;
import net.xxcl.kygl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * User: 夏汛
 * Date: 12-9-27
 * Time: 上午11:50
 */
@Controller
@RequestMapping("/changerank")
public class ChangerankController {

    @Autowired
    private ChangerankService changerankService;
    @Autowired
    private UserService userService;

    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody
    Map<String,Object> save(@ModelAttribute("user")Changerank changerank){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        changerank.setApplytime(new Date());
        String[] oldids = changerank.getOldids().split(",");
        String[] newids = changerank.getNewids().split(",");
        StringBuffer oldnames = new StringBuffer();
        StringBuffer newnames = new StringBuffer();
        for(int i=0;i<oldids.length;i++){
            User user = userService.selectByPrimaryKey(Integer.parseInt(oldids[i]));
            if(user.getIscolleague()){
                oldnames.append(",");
                oldnames.append(user.getUsername());
            } else {
                oldnames.append(",(");
                oldnames.append(user.getUsername());
                oldnames.append(")");
            }
        }
        changerank.setOldnames(oldnames.substring(1));

        for(int i=0;i<newids.length;i++){
            User user = userService.selectByPrimaryKey(Integer.parseInt(newids[i]));
            if(user.getIscolleague()){
                newnames.append(",");
                newnames.append(user.getUsername());
            } else {
                newnames.append(",(");
                newnames.append(user.getUsername());
                newnames.append(")");
            }
        }
        changerank.setNewnames(newnames.substring(1));

        changerankService.insertSelective(changerank);
        responseMap.put("success",true);
        responseMap.put("info","申请成功提交");
        return responseMap;
    }

    @RequestMapping(value = "/list")
    public @ResponseBody Map<String,Object> list(@RequestParam(value = "start",required = false) Integer start,
                                                 @RequestParam(value = "limit",required = false) Integer limit){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();

        int total = userService.countByExample(criteria);
        if(start!=null && limit!=null){
            criteria.setMysqlOffset(start);
            criteria.setMysqlLength(limit);
        }
        criteria.setOrderByClause("t_changerank.state asc,t_changerank.id desc");
        List<Changerank> list = changerankService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    @RequestMapping(value = "/change")
    public @ResponseBody Map<String,Object> change(@RequestParam(value = "id") Integer id,
                                                 @RequestParam(value = "isagree") Integer isagree,
                                                 HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        User user = (User)request.getSession().getAttribute("user");
        Changerank changerank = new Changerank();
        changerank = changerankService.selectByPrimaryKey(id);
        if(changerank.getState()==0){
            //审批人
            changerank.setApplyname(user.getUsername());
            changerank.setAgreetime(new Date());
            changerankService.change(changerank,isagree);
            responseMap.put("info","提交成功");
        } else {
            responseMap.put("info","排名变更已经审批过，请刷新页面重试");
        }
        responseMap.put("success",true);
        return responseMap;
    }
}
